<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2018-04-04
 * Time: 11:48
 */
namespace app\common\model;
use think\Db;
use think\Model;

class User extends Model{
    // 关联用户资料表
    public function userdb(){
        return $this->hasOne('UserData','uid','uid');
    }
    // 关联用户组表
    public function usergroup(){
        return $this->hasOne('AuthGroupAccess','uid','uid');
    }

    /**
     * @param $data 获取注册参数
     * 初始化参数中  uphone用户手机 group_id用户组id(默认为普通用户组) username用户名(若不存在则默认手机号，可修改一次) uniname用户昵称(若不存在，则默认手机号) uemail用户邮箱(可为空) uname用户姓名(可为空)
     * @return 返回是否注册成功，不保存数据
     */
    public function api_reg($data){
        //  初始化必选项
        $newdata['uphone'] = empty($data['uphone']) ? null : $data['uphone'];
        $newdata['group_id'] = empty($data['group_id']) ? '5' : $data['group_id'];
        $newdata['username'] = empty($data['username']) ? null : $data['username'];
        $newdata['uniname'] = empty($data['uniname']) ? null : $data['uniname'];
        $newdata['uemail'] = empty($data['uemail']) ? null : $data['uemail'];
        $newdata['uname'] = empty($data['uname']) ? null : $data['uname'];
        if(empty($newdata['username'])){
            $newdata['username'] = $newdata['uphone'];
            $newdata['uedit'] = '1';
        }
        if(empty($newdata['uniname'])){
            $newdata['uniname'] = $newdata['uphone'];
        }
        $newdata = array_merge($data,$newdata);
        $newdata['upassword'] = pwd($newdata['upassword']);
        //  检测是否需要注册审核
        $webdb = webdb();
        if($webdb['user_status'] == '0'){
            $newdata['status'] = '1';
        }
        //  去除数据表没有的字段
        $fields = Db::query("DESC ".config('database.prefix')."user_data");
        $fieldslist = $newenddata = array();
        foreach ($fields as $k => $v){
            $fieldslist[] = $v['Field'];
        }
        foreach ($newdata as $k => $v){
            if(in_array($k,$fieldslist)){
                $enddata[$k] = $v;
            }
        }
        //  开始写入数据
        if($this->allowField(true)->save($enddata)){
            $this->userdb()->save($enddata);
            $arr = array(
                'uid' => $this->uid,
                'group_id' => $enddata['group_id'],
            );
            $this->usergroup()->save($arr);
            // 检测是否赠送积分
            $arr['username'] = $enddata['username'];
            //  处理注册赠送积分
            if(!empty($webdb['user_reg_money']) || $webdb['user_reg_money'] != '0'){
                $arr['money_class'] = $webdb['user_reg_moneyclass'];
                $addmoney = $webdb['user_reg_money'];
                \model('UserMoney')->add_reg($arr,$addmoney);
            }
            //  通知网站管理员
            \model('Sms')->addsms($arr);
            //  检测推荐是否存在 如存在则写入推荐系统
            if(!empty($data['t'])){
                $puid = \model('UserData')->where('uphone',$data['t'])->find();
                if(!empty($puid)){
                    \model('UserSpread')->newadd($arr['uid'],$puid['uid']);
                }
            }
            return true;
        }
        return false;
    }
    //  API用户登录
    public function api_login($data){
        $user = $this->where('username',$data['username'])->find();
        if(yzpwd($data['upassword'],$user['upassword'])) {
            $users = $user->userdb;
            $users = $users->toArray();
            unset($users['upassword']);
            return $users;
        }else{
            return false;
        }
    }
    //  网页用户登录
    public function loginuser($data){
        $user = $this->where('username',$data['username'])->find();
        if(yzpwd($data['upassword'],$user['upassword'])){
            $users = $user->userdb;
            $users = $users->toArray();
            $users['group_name'] = \model('AuthGroup')->where('id',$users['group_id'])->value('title');
            unset($users['upassword']);
            userCacheSet($user['uid'],'userdb',$users);
            session('userdb',$users);
            return true;
        }else{
            return false;
        }
    }
    //  添加新用户，注册页面来的
    public function adduser($data){
        $data['upassword'] = pwd($data['upassword']);
        $newdata = ([
            'group_id' => '5',
            'username' => $data['uphone'],
            'uniname' => $data['uname'],
            'uname' => $data['uname'],
            'uphone' => $data['uphone'],
            'uphone_yz' => '1',
            'status' => '1',
            'upassword' =>$data['upassword'],
        ]);
        $this->allowField(true)->save($newdata);
        $this->userdb()->save($newdata);
        $arr = array(
            'uid' => $this->uid,
            'group_id' => $newdata['group_id'],
        );
        $this->usergroup()->save($arr);
        $newdata['uid'] = $this->uid;
        if(!empty($data['tuijian']) && isset($data['tuijian'])){
            $add['puid'] = $this->where('username',$data['tuijian'])->value('uid');
            $add['uid'] = $newdata['uid'];
            model('UserSpread')->save($add);
        }
        return $newdata;
    }
    //  添加新用户 微信来的
    //  此方法准备废弃
    public function wxapiadd($data){
        $data['upassword'] = pwd($data['username']);
        $data['group_id'] = '5';
        $data['uphone_yz'] = '1';
        $data['status'] = '1';
        $this->allowField(true)->save($data);
        $this->userdb()->save($data);
        $arr = array(
            'uid' => $this->uid,
            'group_id' => $data['group_id'],
        );
        $this->usergroup()->save($arr);
        return $this->uid;
    }

    /**
     * @param $data 接收要修改的用户名和密码
     * @return bool 返回是否修改成功
     */
    public function repwd($data){
        $user = $this->where('username',$data['username'])->find();
        $data['upassword'] = pwd($data['upassword']);
        if($user->isUpdate(true)->save(['upassword'  => $data['upassword']],['uid' => $user['uid']])){
            if($user['uid'] !== '1'){
                $user->userdb->save(['upassword'  => $data['upassword']],['uid' => $user['uid']]);
            }
            return true;
        }
        return false;
    }
    //  修改用户资料
    public function edit_user($data){
        $user = $this->where('uid',$data['uid'])->find();
        $user = $user->userdb->toArray();
        $user = array_merge($user,$data);
        if(\model('UserData')->allowField(true)->isUpdate(true)->save($user,['uid'=>$user['uid']])){
            $user['group_name'] = \model('AuthGroup')->where('id',$user['group_id'])->value('title');
            unset($user['upassword']);
            userCacheSet($user['uid'],'userdb',$user);
            session('userdb',$user);
            return true;
        }else{
            return false;
        }
    }
    //  添加收藏
    public function shoucang($data){
        $userdata = $this->where('uid',$data['uid'])->find();
        $shoucang = $userdata->userdb['ucollection'];
        $shoucang = explode(',',$shoucang);
        $shoucang = arrayNull($shoucang);
        if(in_array($data['aid'],$shoucang)){
            foreach ($shoucang as $k => $v){
                if($v == $data['aid']){
                    unset($shoucang[$k]);
                }
            }
        }else{
            $shoucang[] = $data['aid'];
        }
        $userdata->userdb['ucollection'] = $shoucang = implode(',',$shoucang);
        if($userdata->userdb->save(['ucollection'  => $shoucang],['uid' => $data['uid']])){
            $userdata = $userdata->userdb;
            $userdata = $userdata->toArray();
            $userdata['group_name'] = \model('AuthGroup')->where('id',$userdata['group_id'])->value('title');
            unset($userdata['upassword']);
            userCacheSet($userdata['uid'],'userdb',$userdata);
            session('userdb',$userdata);
            return true;
        }
        return false;
    }

}